package com.kly.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kly.domain.entity.Message;
import com.kly.domain.entity.User;
import com.kly.domain.vo.MessageVO;
import com.kly.service.MessageService;
import com.kly.mapper.MessageMapper;
import com.kly.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author admin
* @description 针对表【message(消息)】的数据库操作Service实现
* @createDate 2025-05-10 15:09:51
*/
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements MessageService{

    @Resource
    private UserService userService;

    @Override
    public List<MessageVO> getMessageVOList(List<Message> records) {
        if (records != null) {
            return records.stream().map(record -> {
                MessageVO messageVO = new MessageVO();
                messageVO.setId(record.getId());
                messageVO.setSenderId(record.getSenderId());
                User userDb = userService.getById(record.getSenderId());
                messageVO.setSenderName(userDb.getUserName());
                messageVO.setSenderAvatar(userDb.getUserAvatar());
                messageVO.setReceiverType(record.getReceiverType());
                messageVO.setReceiverId(record.getReceiverId());
                messageVO.setContent(record.getContent());
                messageVO.setCreateTime(record.getCreateTime());
                return messageVO;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}




